import { defineStore } from "pinia";
import { ref } from "vue";
import { getUserInfo } from "@/api/login";
export const useUserStore = defineStore(
  "user",
  () => {
    const token = ref("");
    // 设置token
    const setToken = (newToken) => {
      token.value = newToken;
    };
    // 移除token
    const removeToken = () => {
      token.value = "";
    };

    // 用户信息
    const userInfo = ref({});
    // 设置用户信息
    const setUserInfo = async () => {
      const res = await getUserInfo();
      userInfo.value = res.data.data;
    };
    // 移除用户信息
    const removeUserInfo = () => {
      userInfo.value = {};
    };

    return {
      token,
      setToken,
      removeToken,
      userInfo,
      setUserInfo,
      removeUserInfo,
    };
  },
  { persist: true }
);
